import { queryCustomerList, deleteClientUser } from "../../../../service/custom_service"
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    customerList: [

    ],
    requestData: {
      words: "",
      zone: "",//区域
      buyDegree: "",//购房意向程度
      buyHouseCategory: "",//购买类型

    },

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
    this.queryCustomerList();
  },
  //获取我拥有的客户:
  queryCustomerList() {
    wx.showLoading();
    let { words, zone, buyDegree, buyHouseCategory } = this.data.requestData;
    queryCustomerList(words, zone, buyDegree, buyHouseCategory).then(res => {
      wx.hideLoading();
      console.log("JJLog:获取到的用户是:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          customerList: res.data
        })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "查询失败",
        icon: "none"
      })
      console.error("JJLog:queryCustomerList:", err);
    })
  },
  //搜索客户:
  searchAction(e) {
    this.setData({
      'requestData.words': e.detail.searchWord,
    })
    this.queryCustomerList();
  },

  //筛选组件确认返回:
  filterConfirm(e) {
    console.log("JJLog:筛选组件确认返回:" + JSON.stringify(e.detail));

    let fliterData = e.detail;
    for (let key in fliterData) {
      console.log("JJLog:当前的key是:" + key);
      if (fliterData[key] != null && key != "unitPrice" && key != "totalPrice" && key != "area") {
        let value = `requestData.${key}`
        this.setData({
          [value]: fliterData[key] == '不限' ? null : fliterData[key],
        })

      }


    }
    console.log("JJLog:筛选后的requestData===:" + JSON.stringify(this.data.requestData));
    this.queryCustomerList();

  },
  //添加客户
  addCustomer() {
    wx.navigateTo({
      url: '/firstPackage/pages/customManager/addCustomer/addCustomer',
    })
  },

  //跟进客户
  followCustomAction(e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/firstPackage/pages/customManager/customerFllowDetail/customerFllowDetail?id=' + id,
    })
  },
  //删除一个客户
  cancelOneCustomer(e) {
    let id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '是否删除当前客户',
      success: res => {
        if (res.confirm) {
          this.cancelCustomerRequest(id);
        }
      }
    })

  },
  //删除客户请求
  cancelCustomerRequest(id) {
    wx.showLoading();
    deleteClientUser(id).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        this.queryCustomerList();
      }
    }).catch(err => {
      console.error("JJLog:deleteClientUser", err);
      wx.hideLoading();
      wx.showToast({
        title: err || "操作失败",
        icon: "none"
      })
    })
  },

  //跳转到客户详情:
  pushToCustomDetail(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/firstPackage/pages/customManager/customerDetail/customerDetail?id=' + id,
    })
  },






})